﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Xejen.Communication
{
    /// <summary>
    /// 表示具有<typeparamref name="T"/>类型返回值的通讯数据结构对象
    /// </summary>
    /// <typeparam name="T">通讯交互的返回值类型</typeparam>
    public class Result<T> : Result
    {
        /// <summary>
        /// 返回的数据
        /// </summary>
        public T ReturnData { get; set; }

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public Result()
        {

        }

        /// <inheritdoc/>
        public override object GetSerializableObject()
        {
            if (this.IsSuccess)
            {
                return new
                {
                    Code = this.Code,
                    IsSuccess = this.IsSuccess,
                    ReturnData = this.ReturnData,
                };
            }
            else
            {
                return new
                {
                    Code = this.Code,
                    IsSuccess = this.IsSuccess,
                    ErrorMessage = this.Exception?.Message,
                    StackTrace = this.Exception?.StackTrace
                };
            }
        }
    }
}
